package com.acmen.easyapi.model;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.ArrayList;
import java.util.List;

/**
 * @author acmen
 * 远程调用
 */
public class Rpc {
    /**
     * 依赖
     */
    private Dependency dependency;
    /**
     * 远程调用接口
     */
    private String rpcInterface;
    /**
     * 方法名
     */
    private String methodName;
    /**
     * 入参类型
     */
    private List<String> parameterTypes;
    /**
     * 入参
     */
    private List<String> parameters;
    /**
     * 回参
     */
    private String returnType;

    public Dependency getDependency() {
        return dependency;
    }

    public void setDependency(Dependency dependency) {
        this.dependency = dependency;
    }

    public String getRpcInterface() {
        return rpcInterface;
    }

    public void setRpcInterface(String rpcInterface) {
        this.rpcInterface = rpcInterface;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public List<String> getParameterTypes() {
        return parameterTypes;
    }

    public void setParameterTypes(List<String> parameterTypes) {
        this.parameterTypes = parameterTypes;
    }

    public String getReturnType() {
        return returnType;
    }

    public void setReturnType(String returnType) {
        this.returnType = returnType;
    }

    public List<String> getParameters() {
        return parameters;
    }

    public void setParameters(List<String> parameters) {
        this.parameters = parameters;
    }
    @JSONField(deserialize = false,serialize = false)
    public String getFieldString(){
        int idx = rpcInterface.lastIndexOf(".");
        String fieldType = rpcInterface.substring(idx+1);
        String fieldName = fieldType.substring(0,1).toLowerCase()+fieldType.substring(1);
        String fieldString = fieldType +" "+fieldName;
        return fieldString;
    }

    @JSONField(deserialize = false,serialize = false)
    public List<String> getCodes(){
        List<String> codes = new ArrayList<>();
        for (int i=0;i<parameterTypes.size();i++) {
            String parameter = parameters.get(i);
            String type = parameterTypes.get(i);
            String typeName = getSimpleName(type);
            String code = typeName + " " + parameter;
            if(typeName.equals("Map")){
                code += " = new HashMap()";
            }else if(typeName.equals("List")){
                code += " = new ArrayList()";
            }else if(typeName.equals("String")){
                code += " = \"Abc123\"";
            }else if(typeName.equals("Integer")||typeName.equals("int")||typeName.equals("short")){
                code += " = 10";
            }else if(typeName.equalsIgnoreCase("Float")||typeName.equalsIgnoreCase("Double")){
                code += " = 10.0";
            }else {
                code += " = new "+typeName+"()";
            }
            codes.add(code);
        }
        String returnType1 = getSimpleName(returnType);
        String returnObject;
        if(returnType1.contains("&lt;")){
            int index = returnType1.indexOf("&lt;");
            String str1 = returnType1.substring(0,index);
            String rt = str1.substring(str1.lastIndexOf(".")+1);
            returnObject = rt.substring(0,1).toLowerCase()+rt.substring(1);
        }else{
            returnObject = returnType1.substring(0,1).toLowerCase()+returnType1.substring(1);
        }
       String rpcInterfaceName = rpcInterface.substring(rpcInterface.lastIndexOf(".")+1);
        rpcInterfaceName = rpcInterfaceName.substring(0,1).toLowerCase()+rpcInterfaceName.substring(1);
        String invokeString = returnType1 +" "+returnObject +" = " +rpcInterfaceName+"."+methodName+"(";
        for (int i=0;i<parameters.size();i++) {
            if(i>0){
                invokeString += ",";
            }
            invokeString += parameters.get(i);
        }
        invokeString += ")";
        codes.add(invokeString);
        return codes;
    }
    @JSONField(deserialize = false,serialize = false)
    public List<String> getImports() {
        List<String> imports = new ArrayList<>();
        imports.add(rpcInterface);
        for (String clazz : parameterTypes) {
            imports.addAll(genClass(clazz));
        }
        imports.addAll(genClass(returnType));
        return imports;
    }

    private List<String> genClass(String typeString){
        typeString = typeString.trim();
        List<String> classList = new ArrayList<>();
        int index = typeString.indexOf("<");
        if (index > 0) {
            classList.add(typeString.substring(0, index));
            List list = genClass(typeString.substring(index+1,typeString.length()-1));
            classList.addAll(list);
        } else {
            classList.add(typeString);
        }
        return classList;
    }

    private String getSimpleName(String className){
        StringBuffer simpleName = new StringBuffer();
        StringBuffer temp = new StringBuffer();
        for(char c : className.toCharArray()){
            if(c=='.'){
                temp.delete(0,temp.length());
            }else if(c=='<'){
                simpleName.append(temp.toString()+"&lt;");
                temp.delete(0,temp.length());
            }else if(c=='>'){
                simpleName.append(temp.toString()+"&gt;");
                temp.delete(0,temp.length());
            }else{
                temp.append(c);
            }
        }
        simpleName.append(temp.toString());
        return simpleName.toString();
    }
}
